package com.fishery.processing.mapper;

import com.fishery.processing.entity.Craft;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
public interface CraftMapper extends BaseMapper<Craft> {

    @Update("UPDATE t_processing_product_craft SET sort = sort + 1 WHERE product_id = #{productId} AND sort >= #{sort};")
    public int increaseSort(@Param("productId") String productId, @Param("sort") Integer sort);

    @Update("UPDATE t_processing_product_craft SET sort = sort - 1 WHERE product_id = #{productId} AND sort > #{sort};")
    public int decreaseSort(@Param("productId") String productId, @Param("sort") Integer sort);

    @Update("UPDATE t_processing_product_craft t SET craft_id = #{craftId} WHERE product_id = #{productId} AND sort = #{sort};")
    public int updateCraftOfProduct(@Param("productId") String productId,@Param("craftId") String craftId
                                    ,@Param("sort") Integer sort);

    @Insert("INSERT INTO t_processing_product_craft VALUES(#{productId},#{craftId},#{sort});")
    public int addCraftOfProduct(@Param("productId") String productId,@Param("craftId") String craftId
                                    ,@Param("sort") Integer sort);

    @Delete("DELETE FROM t_processing_product_craft WHERE sort = #{sort} AND product_id = #{productId};")
    public int removeCraftOfProduct(@Param("productId") String productId,@Param("sort") Integer sort);

    @Select("SELECT COUNT(*) FROM t_processing_product_craft WHERE product_id = #{productId};")
    public long getCraftCountOfProduct(@Param("productId") String productId);

    @Delete("DELETE FROM t_processing_product_craft WHERE product_id = #{productId};")
    public int removeAllCraftOfProduct(@Param("productId") String productId);

    //分页查找加工产品的工艺列表
    @Select("SELECT c.*,pc.sort FROM t_processing_craft c INNER JOIN t_processing_product_craft pc WHERE c.id = pc.craft_id AND pc.product_id = #{productId} ORDER BY pc.sort LIMIT #{startNo},#{size};")
    public List<Craft> findCraftsOfProduct(@Param("productId") String productId,@Param("startNo") Integer startNo
                                            ,@Param("size") Integer size);

    //(全部)查找加工产品的工艺列表
    @Select("SELECT c.*,pc.sort FROM t_processing_craft c INNER JOIN t_processing_product_craft pc WHERE c.id = pc.craft_id AND pc.product_id = #{productId} ORDER BY pc.sort;")
    public List<Craft> findAllCraftsOfProduct(@Param("productId") String productId);

    @Select("SELECT craft_id FROM t_processing_product_craft WHERE product_id = #{productId}")
    public List<String> getCraftIdOfProduct(String productId);
}
